﻿using Microsoft.EntityFrameworkCore;
using Microsoft.VisualBasic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TR.AppService.Dto.XSddDto;
using TR.Domain.Entity;
using TR.Domain.Entity.XSDDGL;
using TR.Domain.Repository.XSDD_GuanLiRepository;

namespace TR.EFCore.Repository.XSDD_GuanLiRepository
{
    public class XsddRePository : IXsddRepository
    {
        private readonly MyContext context;
        public int fyzts;

        public XsddRePository(MyContext context)
        {
            this.context = context;
        }

        //销售订单查询
        public async Task<List<XSdd>> XsddFenYeSelect(int mys, int dqy, string? qsj, string? hsj, XSdd xs)
        {
            var query = context.XSdd.AsQueryable();
            query = query.Where(p => p.is_del == 0);

            if (!string.IsNullOrEmpty(qsj) && !string.IsNullOrEmpty(hsj))
            {
                query = query.Where(p => string.Compare(p.sale_date, qsj) >= 0 && string.Compare(p.sale_date, hsj) <= 0);
            }

            //根据条件查询 进货单号、单据状态、供应商
            if (xs != null)
            {
                //单据编号
                if (!string.IsNullOrEmpty(xs.sale_code))
                {
                    query = query.Where(p => EF.Functions.Like(p.sale_code, "%" + xs.sale_code + "%"));
                }
                if (xs.sure_state != '\0')
                {
                    query = query.Where(p => p.sure_state == xs.sure_state);
                }
                if (!string.IsNullOrEmpty(xs.cust_name))
                {
                    query = query.Where(p => p.cust_name == xs.cust_name);
                }
            }
            var result = await query.OrderByDescending(p => p.create_time).Skip(mys * (dqy - 1)).Take(mys).ToListAsync();//异步使用ToListAsync

            fyzts = await query.CountAsync(); // 获取符合查询条件的记录数

            return result;
        }

        public async Task<int> XsCount()
        {
            return fyzts;
        }

        //批量删除
        public async Task<bool> XsddDelete(List<int> isdel)
        {
            try
            {
                var query = context;

                var xsdel = query.wh_Sale_h.Where(p => isdel.Contains(p.Id)).ToList();

                foreach (var id in xsdel)
                {
                    id.is_del = 1;
                    query.Entry(id).State = EntityState.Modified;
                }
                query.SaveChanges();

                return true;
            }
            catch (Exception)
            {
                Console.WriteLine("错误");
                return false;
            }
        }

        //批量审核
        public async Task<bool> XsddShenHe(List<int> Bian, string states, string sureman, string suredate)
        {
            try
            {
                var query = context;

                var xssh = query.wh_Sale_h.Where(p => Bian.Contains(p.Id)).ToList();

                foreach (var id in xssh)
                {
                    id.sure_state = states;
                    id.sure_man = sureman;
                    id.sure_date = suredate;
                    query.Entry(id).State = EntityState.Modified;
                }
                query.SaveChanges();

                return true;
            }
            catch (Exception)
            {
                Console.WriteLine("错误");
                return false;
            }
        }

        //批量撤销
        public async Task<bool> XsddCheXiao(List<int> Bian)
        {
            try
            {
                var query = context;

                var xssh = query.wh_Sale_h.Where(p => Bian.Contains(p.Id)).ToList();

                foreach (var id in xssh)
                {
                    id.sure_state = "2";
                    id.sure_man = "";
                    id.sure_date = "";
                    query.Entry(id).State = EntityState.Modified;
                }
                query.SaveChanges();

                return true;
            }
            catch (Exception)
            {
                Console.WriteLine("错误");
                return false;
            }
        }

        //客户信息查询
        public async Task<List<bs_Customer>> KHSelect()
        {
            var query = context.bs_Customer.AsQueryable();

            query = query.Where(p => p.is_del == 0);

            return query.ToList();
        }

        //客户编号查询相关信息
        public async Task<bs_Customer> KHBHSelect(string bh)
        {
            var query = context;

            var result = await query.bs_Customer.Where(p => p.cust_code == bh).FirstOrDefaultAsync();

            return result;
        }

        //销售新增
        public async Task<bool> XsddInsert(XsddJcxx jcxx, List<XsddXsmx> xsmx)
        {
            try
            {
                var query = context;

                var wsh = new wh_Sale_h()
                {
                    sale_code = jcxx.sale_code,
                    sale_state = '0',
                    sale_date = jcxx.sale_date,
                    cust_code = jcxx.cust_code,
                    link_man = jcxx.link_man,
                    link_way = jcxx.link_way,
                    sale_duty_man = jcxx.sale_duty_man,
                    create_by = jcxx.create_by,
                    create_time = jcxx.create_time,
                    sure_man = "",
                    sure_date = "",
                    sure_state = "2",
                    is_del = 0
                };

                foreach (var id in xsmx)
                {
                    var wsd = new wh_Sale_d()
                    {
                        sale_code = jcxx.sale_code,
                        item_code = id.item_code,
                        model_code = id.model_code,
                        unit = id.unit,
                        item_type = "1",
                        inbill3_num = 500,
                        sale_num = id.sale_num,
                        return_num = 0,
                        outbill_num = 0,
                        sale_price = id.sale_price,
                        is_del = 0
                    };
                    query.wh_Sale_h.Add(wsh);
                    query.wh_Sale_d.Add(wsd);
                }
                await query.SaveChangesAsync();
                return true;
            }
            catch (Exception)
            {
                Console.WriteLine("错误");
                return false;
            }
        }

        //销售订单修改
        public async Task<bool> XsddUpdate(XsddJcxx jcxx, List<XsddXsmx> xsmx)
        {
            try
            {
                var query = context;

                var wsh = query.wh_Sale_h.FirstOrDefault(p => p.sale_code == jcxx.sale_code);
                if (wsh != null)
                {
                    wsh.sale_date = jcxx.sale_date;
                    wsh.cust_code = jcxx.cust_code;
                    wsh.link_man = jcxx.link_man;
                    wsh.link_way = jcxx.link_way;
                    wsh.sale_duty_man = jcxx.sale_duty_man;

                    var wbohdel = query.wh_Sale_d.Where(p => p.sale_code == jcxx.sale_code).ToList();

                    foreach (var mx in xsmx)
                    {
                        var wsd = new wh_Sale_d()
                        {
                            sale_code = jcxx.sale_code,
                            item_code = mx.item_code,
                            model_code = mx.model_code,
                            unit = mx.unit,
                            item_type = "1",
                            inbill3_num = 500,
                            sale_num = mx.sale_num,
                            return_num = 0,
                            outbill_num = 0,
                            sale_price = mx.sale_price,
                            is_del = 0
                        };
                        query.wh_Sale_h.Update(wsh);
                        query.wh_Sale_d.RemoveRange(wbohdel);
                        query.wh_Sale_d.Add(wsd);
                    }
                    await query.SaveChangesAsync();
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception)
            {
                Console.WriteLine("错误");
                return false;
            }
        }

        //销售订单基本信息查询
        public async Task<wh_Sale_h> XsddJbxxSelect(string tj)
        {
            var query = await context.wh_Sale_h.Where(p => p.sale_code == tj).FirstOrDefaultAsync();

            return query;
        }

        //销售详情
        public async Task<List<XsddXsxqCX>> XsddXsxqSelect(string tj)
        {
            var query = context.XsddXsxqCX.AsQueryable();

            query = query.Where(p => p.sale_code == tj);

            return query.ToList();
        }

        //销售订单添加商品查询
        public async Task<List<XsddTjSP>> XsddTjSPSelect()
        {
            var query = context.XsddTjSP.AsQueryable().ToList();

            return  query;
        }
    }
}
